import org.openjdk.jol.info.ClassLayout;

import java.util.ArrayList;
import java.util.List;

import static java.lang.System.*;

/**
 *
 *
 *
 *
 * -XX:BiasedLockingStartupDelay=0 -XX:BiasedLockingBulkRebiasThreshold=4 -XX:BiasedLockingBulkRevokeThreshold=8
 */
public class BiasLockDemo2 {
    public static String aaa = "1";
    public static void main(String[] args) throws InterruptedException {
        print(new A(),1);
        //创造100个偏向线程t1的偏向锁
        List<A> listA = new ArrayList<>();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 15; i++) {
                A a = new A();
                synchronized (a) {
                    listA.add(a);
                }
            }
        });
        t1.start();
        t1.join();

        for (int i1 = 0; i1 < listA.size(); i1++) {
            print(listA.get(i1), i1 + 1);
        }

        out.println("\n\n--------------t2----------------\n\n");

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 15; i++) {
                A a = listA.get(i);
                synchronized (a) {
                        print(a,i+1);
                }
            }
        });
        t2.start();
        t2.join();

        out.println("\n\n--------------after t2----------------\n\n");
        for (int i1 = 0; i1 < listA.size(); i1++) {
            print(listA.get(i1), i1 + 1);
        }


        out.println("\n\n--------------t3----------------\n\n");

        Thread t3 = new Thread(() -> {
            for (int i = 5; i < 7; i++) {
                A a = listA.get(i);
                synchronized (a) {
                        print(a,i+1);
                }
            }
        });
        t3.start();
        t3.join();

        out.println("\n\n--------------after t3----------------\n\n");
        for (int i1 = 0; i1 < listA.size(); i1++) {
            print(listA.get(i1), i1 + 1);
        }


        print(new A(), "\n\n创建新对象\n");

    }

    public static String print(Object a, Object prefix) {
        String s = ClassLayout.parseInstance(a).toPrintable();
        String[] split = s.split("\n");
        out.println(prefix + "\t:" + split[2]);
        return split[2];
    }

    public static String print(Object a) {
        String s = ClassLayout.parseInstance(a).toPrintable();
        String[] split = s.split("\n");
        out.println(split[2]);
        return split[2];
    }

    public static void sleep(int i) {
        try {
            Thread.sleep(i);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public static class A {

    }
}
